/*
Copyright 2024.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
	"context"

	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/types"

	"k8s.io/apimachinery/pkg/runtime"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/log"

	devcontainer_v1 "devstar.cn/DevcontainerApp/api/v1"
	devcontainer_controller_utils "devstar.cn/DevcontainerApp/internal/controller/utils"
	apps_v1 "k8s.io/api/apps/v1"
	core_v1 "k8s.io/api/core/v1"
	k8s_sigs_controller_runtime_utils "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// DevcontainerAppReconciler reconciles a DevcontainerApp object
type DevcontainerAppReconciler struct {
	client.Client
	Scheme *runtime.Scheme
}

// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps/finalizers,verbs=update
// +kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=create;delete;get;list;watch
// +kubebuilder:rbac:groups="",resources=services,verbs=create;delete;get;list;watch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// Modify the Reconcile function to compare the state specified by
// the DevcontainerApp object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/reconcile
func (r *DevcontainerAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	logger := log.FromContext(ctx)
	var err error

	// 1. 读取缓存中的 DevcontainerApp
	app := &devcontainer_v1.DevcontainerApp{}
	err = r.Get(ctx, req.NamespacedName, app)
	if err != nil {
		// 当 CRD 资源 “DevcontainerApp” 被删除后，直接返回空结果，跳过剩下步骤
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}

	// 2. 根据 DevcontainerApp 配置信息进行处理
	// 2.1 StatefulSet 处理
	statefulSet := devcontainer_controller_utils.NewStatefulSet(app)
	err = k8s_sigs_controller_runtime_utils.SetControllerReference(app, statefulSet, r.Scheme)
	if err != nil {
		return ctrl.Result{}, err
	}

	// 2.2 查找 集群中同名称的 StatefulSet
	statefulSetInNamespace := &apps_v1.StatefulSet{}
	err = r.Get(ctx, req.NamespacedName, statefulSetInNamespace)
	if err != nil {
		if !errors.IsNotFound(err) {
			return ctrl.Result{}, err
		}
		err = r.Create(ctx, statefulSet)
		if err != nil && !errors.IsAlreadyExists(err) {
			logger.Error(err, "Failed to create StatefulSet")
			return ctrl.Result{}, err
		}
	} else {
		// 若 StatefulSet.Status.readyReplicas 变化，则更新 DevcontainerApp.Status.Ready 域（mark/un-mark）
		if statefulSetInNamespace.Status.ReadyReplicas > 0 {
			app.Status.Ready = true
			if err := r.Status().Update(ctx, app); err != nil {
				logger.Error(err, "Failed to update DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready)
				return ctrl.Result{}, err
			}
			logger.Info("DevContainer is READY", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas)
		} else {
			// app.Status.Ready = false
			// if err := r.Status().Update(ctx, app); err != nil {
			// 	logger.Error(err, "Failed to un-mark DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready)
			// 	return ctrl.Result{}, err
			// }
			logger.Info("DevContainer is NOT ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas)
		}

		// 这里会反复触发更新
		// 原因：在 SetupWithManager方法中，监听了 StatefulSet ，所以只要更新 StatefulSet 就会触发
		//     此处更新和 controllerManager 更新 StatefulSet 都会触发更新事件，导致循环触发
		//修复方法：加上判断条件，仅在 app.Spec.StatefulSet.Image != statefulSet.Spec.Template.Spec.Containers[0].Image 时才更新 StatefulSet
		if app.Spec.StatefulSet.Image != statefulSet.Spec.Template.Spec.Containers[0].Image {
			if err := r.Update(ctx, statefulSet); err != nil {
				return ctrl.Result{}, err
			}
		}
	}

	// 2.2 Service 处理
	service := devcontainer_controller_utils.NewService(app)
	if err := k8s_sigs_controller_runtime_utils.SetControllerReference(app, service, r.Scheme); err != nil {
		return ctrl.Result{}, err
	}
	serviceInCluster := &core_v1.Service{}
	err = r.Get(ctx, types.NamespacedName{Name: app.Name, Namespace: app.Namespace}, serviceInCluster)
	if err != nil {
		if !errors.IsNotFound(err) {
			return ctrl.Result{}, err
		}
		err = r.Create(ctx, service)
		if err == nil {
			// 创建 NodePort Service 成功只执行一次 ==> 将NodePort 端口分配信息更新到 app.Status
			logger.Info("[DevStar][DevContainer] NodePort Assigned", "nodePortAssigned", service.Spec.Ports[0].NodePort)

			app.Status.NodePortAssigned = uint16(service.Spec.Ports[0].NodePort)
			if err := r.Status().Update(ctx, app); err != nil {
				logger.Error(err, "Failed to update NodePort of DevcontainerApp", "nodePortAssigned", service.Spec.Ports[0].NodePort)
				return ctrl.Result{}, err
			}
		} else if !errors.IsAlreadyExists(err) {
			logger.Error(err, "Failed to create DevcontainerApp NodePort Service", "nodePortServiceName", service.Name)
			return ctrl.Result{}, err
		}
	}
	return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *DevcontainerAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&devcontainer_v1.DevcontainerApp{}).
		Owns(&apps_v1.StatefulSet{}).
		Owns(&core_v1.Service{}).
		Complete(r)
}
